﻿// ComponentDlg.cpp: 实现文件
//

#include "pch.h"
#include "Steam.h"
#include "ComponentDlg.h"
#include "afxdialogex.h"
#include "PropertyData.h"

// CComponentDlg 对话框

IMPLEMENT_DYNAMIC(CComponentDlg, CDialogEx)

CComponentDlg::CComponentDlg(char _components[ncmax][refpropcharlength],  long _ncomp, CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_COMPONENT_DIALOG, pParent)
{
	ncomp = _ncomp;
	for (int i = 0; i < ncomp; i++)
	{
		strcpy_s(components[i], sizeof components[i] / sizeof components[i][0], _components[i]);
	}

	isModified = false;
}

CComponentDlg::~CComponentDlg()
{
}

void CComponentDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CComponentDlg, CDialogEx)
	ON_BN_CLICKED(IDC_ADD_BUTTON, &CComponentDlg::OnBnClickedAddButton)
	ON_BN_CLICKED(IDC_REMOVE_BUTTON, &CComponentDlg::OnBnClickedRemoveButton)
END_MESSAGE_MAP()


// CComponentDlg 消息处理程序


BOOL CComponentDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	CListBox* listBox = (CListBox*)GetDlgItem(IDC_COMPONENT_LIST);
	CListBox* selListBox = (CListBox*)GetDlgItem(IDC_SELECTED_LIST);
	for (size_t i = 0; i < 121; i++)
	{
		CString strItem;
		if (strcmp(componentList[i][0], componentList[i][3]) == 0)
		{
			strItem = CString(componentList[i][0]);
		}
		else
		{
			strItem += CString(componentList[i][0]);
			strItem += "(";
			strItem += CString(componentList[i][3]);
			strItem += +")";
		}
		
		listBox->AddString(strItem);
	}
	for (int i = 0; i < ncomp; i++)
	{
		for (int j = 0; j < 121; j++)
		{
			if (strcmp(components[i], componentList[j][1]) == 0)
			{
				nSels.push_back(j);
				CString strItem;
				if (strcmp(componentList[j][0], componentList[j][3]) == 0)
				{
					strItem = CString(componentList[j][0]);
				}
				else
				{
					strItem += CString(componentList[j][0]);
					strItem += "(";
					strItem += CString(componentList[j][3]);
					strItem += +")";
				}
				selListBox->AddString(strItem);
			}
		}
	}
	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void CComponentDlg::OnBnClickedAddButton()
{
	// TODO: 在此添加控件通知处理程序代码
	CListBox* selListBox = (CListBox*)GetDlgItem(IDC_SELECTED_LIST);
	CListBox* listBox = (CListBox*)GetDlgItem(IDC_COMPONENT_LIST);
	
	int nSel = listBox->GetCurSel();
	CString strSelItem;
	listBox->GetText(nSel, strSelItem);
	if (selListBox->FindString(-1, strSelItem) == LB_ERR)
	{
		CString strItem;
		if (strcmp(componentList[nSel][0], componentList[nSel][3]) == 0)
		{
			strItem = CString(componentList[nSel][0]);
		}
		else
		{
			strItem += CString(componentList[nSel][0]);
			strItem += "(";
			strItem += CString(componentList[nSel][3]);
			strItem += +")";
		}
		selListBox->AddString(strItem);
		nSels.push_back(nSel);
		ncomp = selListBox->GetCount();
		for (int i = 0; i < nSels.size(); i++)
		{
			strcpy_s(components[i], sizeof components[i] / sizeof components[i][0], componentList[nSels[i]][1]);
		}

		isModified = true;
	}
	else
	{
		AfxMessageBox(L"该组分已添加");
	}
}


void CComponentDlg::OnBnClickedRemoveButton()
{
	// TODO: 在此添加控件通知处理程序代码
	CListBox* selListBox = (CListBox*)GetDlgItem(IDC_SELECTED_LIST);
	nSels.erase(nSels.begin() + selListBox->GetCurSel());
	selListBox->DeleteString(selListBox->GetCurSel());
	ncomp = selListBox->GetCount();
	for (int i = 0; i < nSels.size(); i++)
	{
		strcpy_s(components[i], sizeof components[i] / sizeof components[i][0], componentList[nSels[i]][1]);
	}
	isModified = true;
}
